package com.huike.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.service.TbClueService2;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class TbClueService2Impl2 extends ServiceImpl<TbClueMapper, TbClue>
        implements TbClueService2 {
    private final TbBusinessMapper tbBusinessMapper;
    private final TbClueMapper tbClueMapper;
    private final TbAssignRecordMapper tbAssignRecordMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clueChangeBusiness(Long id) {
        TbClue tbClue = tbClueMapper.selectById(id);
        if(tbClue == null){
            throw new BaseException("没有找到线索");
        }
        LambdaQueryWrapper<TbAssignRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null, TbAssignRecord::getAssignId, id)
                .eq(TbAssignRecord::getType,0)
                .eq(TbAssignRecord::getUserName, SecurityUtils.getUsername());
        TbAssignRecord tbAssignRecord = tbAssignRecordMapper.selectOne(wrapper);
        if(tbAssignRecord == null){
            throw new BaseException("没有找到跟进记录");
        }

        //填充tbBusiness字段
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setName(tbClue.getName());
        tbBusiness.setPhone(tbClue.getPhone());
        tbBusiness.setChannel(tbClue.getChannel());
        tbBusiness.setActivityId(tbClue.getActivityId());
        tbBusiness.setSex(tbClue.getSex());
        tbBusiness.setAge(tbClue.getAge());
        tbBusiness.setWeixin(tbClue.getWeixin());
        tbBusiness.setQq(tbClue.getQq());
        tbBusiness.setLevel(tbClue.getLevel());
        tbBusiness.setSubject(tbClue.getSubject());
        tbBusiness.setCreateBy(SecurityUtils.getUsername());
        tbBusiness.setCreateTime(LocalDateTime.now().toString());
        tbBusiness.setStatus("1");
        tbBusiness.setClueId(tbClue.getId());
        tbBusiness.setTransfer(tbClue.getTransfer());
        tbBusinessMapper.insert(tbBusiness);
        LambdaQueryWrapper<TbBusiness> businessWrapper = new LambdaQueryWrapper<>();
        businessWrapper.eq(tbClue.getName() != null, TbBusiness::getName, tbClue.getName());
        TbBusiness tbBusiness2 = tbBusinessMapper.selectOne(businessWrapper);
        if(tbBusiness2 == null){
            throw new BaseException("没有找到商机");
        }

        tbAssignRecord.setAssignId(tbBusiness2.getId());
        tbAssignRecord.setType("1");
        tbAssignRecordMapper.updateById(tbAssignRecord);
    }
}
